package com.company;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class ADBExecutor {
    public static final int ANDROID_PORT = 30000;
    public static final int HOST_PORT = 35000;

    private final String adb_directory;

    public ADBExecutor(String adb_directory) {
        this.adb_directory = adb_directory;
    }

    //得到当前连接上的设备的序列号
    public String execAdbDevice() {
        System.out.println("adb devices");
        Process proc = null;
        try {
            proc = new ProcessBuilder(this.adb_directory, "devices").start();
            proc.waitFor();
        } catch (IOException ioe) {
            ioe.printStackTrace();
        } catch (InterruptedException ire) {
            ire.printStackTrace();
        }

        String devices_result = this.collectResultFromProcess(proc);
        String[] device_id_list = devices_result.split("\\r?\\n");

        if (device_id_list.length <= 1) {
            System.out.println("No Devices Attached.");
            return null;
        }

        /**
         * collect the online devices
         */
        String str_device_id = null;
        String device = null;
        String[] str_device_id_parts = null;
        // ignore the first line which is "List of devices attached"
        str_device_id = device_id_list[1];
        str_device_id_parts = str_device_id.split("\\s+");
        // add the online device
        if (str_device_id_parts[1].equals("device")) {
            device = str_device_id_parts[0];
            System.out.println(device);
            return device;
        }
        return null;
    }

    //具体的映射过程
    public void execAdbSingleDevicePortForward(String device_id, int host_port, int to_port) {
        System.out.println("adb -s " + device_id + " forward tcp:" + host_port + " tcp:" + to_port);

        Process proc = null;
        try {
            proc = new ProcessBuilder(this.adb_directory, "-s", device_id, "forward", "tcp:" + host_port, "tcp:" + to_port).start();
            proc.waitFor();
        } catch (IOException ioe) {
            ioe.printStackTrace();
        } catch (InterruptedException ire) {
            ire.printStackTrace();
        }
    }

    //手机命令执行之后的结果
    private String collectResultFromProcess(Process proc) {
        StringBuilder sb_result = new StringBuilder();

        BufferedReader stdInput = new BufferedReader(new InputStreamReader(proc.getInputStream()));
        BufferedReader stdError = new BufferedReader(new InputStreamReader(proc.getErrorStream()));
        String result_line = null;

        try {
            while ((result_line = stdInput.readLine()) != null) {
                sb_result.append(result_line);
                sb_result.append("\n");
            }

            while ((result_line = stdError.readLine()) != null) {
                sb_result.append(result_line);
                sb_result.append("\n");
            }
        } catch (IOException ioe) {
            ioe.printStackTrace();
        }

        return sb_result.toString();
    }
}
